package cn.zyg.controller;

import cn.zyg.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;
    @GetMapping("/getUserById/{id}")
    @HystrixCommand(fallbackMethod = "getUserByIdFallback")
    public User getUserById(@PathVariable("id") Long id) {
        // String url = "http://localhost:10020/user/getUserById/" + id;
        // 要发挥集群的作用，负载均衡，那么就必须通过服务器名称调用
        // user-server就包含了它里面的所有服务： 10020 10021
        String url = "http://user-server/user/getUserById/" + id;
        User user = restTemplate.getForObject(url, User.class);
        return user;

    }

    // 响应托底数据的方法
    public User getUserByIdFallback(Long id) {
        return new User(-1L,"触发熔断");

    }
}
